package com.ybkj.daijia.api.apirest;

import com.ybkj.daijia.serverUtils.HttpClientUtil;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.api.driver.transport.ResultTransport;
import com.ybkj.daijia.api.util.ErrorConstantV4;
import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.service.CompanyService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller("ApiCompanyProcessController")
@RequestMapping(value = "api/company/")
public class CompanyProcessController {

    @Autowired
    private CompanyService companyService;

    @Autowired
    private SettingUtils settingUtils;


    /**
     * 根据经纬度查询公司
     *
     * @param lng
     * @param lat
     * @return
     */
    @RequestMapping(value = "findByLngAndLat", method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> findByLngAndLat(Double lng, Double lat) {
        Company mainCompany = companyService.getMain();

        List<Company> companys = new ArrayList<Company>();
        companys = companyService.findByAreaCityName(HttpClientUtil.getCityByLngAndLat(lng, lat));

        Map<String, Object> map = new HashMap<String, Object>();

        map.put("main", mainCompany);
        map.put("others", companys);

        return map;

    }

    @RequestMapping(value = "findByCity", method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> findByCity(String city) {
        Company mainCompany = companyService.getMain();

        List<Company> companys = new ArrayList<Company>();
        companys = companyService.findByAreaCityName(city);

        Map<String, Object> map = new HashMap<String, Object>();

        map.put("main", mainCompany);
        map.put("others", companys);

        return map;

    }

    /**
     * 根据名字查询公司
     *
     * @param phone
     * @return
     */
    @RequestMapping(value = "findCompanyByAcKey", method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> findCompanyByAcKey(String acKey) {
        Company mainCompany = companyService.getMain();

        Company company = null;
        if (StringUtils.isNotBlank(acKey)) {
            company = companyService.findByAppKey(acKey);
        }

        Map<String, Object> map = new HashMap<String, Object>();
        if (company == null) {
            map.put("company", mainCompany);
        } else {
            map.put("company", company);
        }
        return map;
    }

//	@RequestMapping(value="findProtectContent")
//	public @ResponseBody ResultTransport findProtectContent(Long companyId){
//		
//		if(null == companyId) {
//			return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
//		}
//		Company company = companyService.findOne(companyId);
//		try {
//			
//			Setting setting=settingUtils.get();
//			
//			Map<String, Object> map=new HashMap<String, Object>();
//			map.put("protectContent", company.getProtectFeeContent());
//			ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
//			instance.setData(map);
//			
//			return instance;
//			
//		} catch (Exception e) {
//			return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
//		}
//		
//	}

    /**
     * 保价说明
     *
     * @param companyId 客户所在公司id
     */
    @RequestMapping(value = "findProtectContent")
    public String findProtectContent(Long companyId, Model model) {
        Company company = companyService.findOne(companyId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("protectContent", company.getProtectFeeContent());
        ResultTransport instance = ResultTransport
            .getErrorBy(ErrorConstantV4.SUCCESS);
        instance.setData(map);
        String protectFeeContent = null;
        protectFeeContent = company.getProtectFeeContent();
        if (StringUtils.isBlank(protectFeeContent)) {
            protectFeeContent = companyService.getMain().getProtectFeeContent();
        }
        model.addAttribute("protectFeeContent", protectFeeContent);
        return "errand/protectment";
    }

    /**
     * 获取主公司
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "main", method = {RequestMethod.GET})
    public Map<String, Object> getMainCompany() {
        Map<String, Object> data = new HashMap<String, Object>();
        Company mainCompany = companyService.getMain();
        data.put("main", mainCompany);
        return data;
    }
}
